package com.hanbang.mrcm.cdker_type;

import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hanbang.mrcm.R;
import com.hanbang.mrcm.base.BaseActivity;
import com.hanbang.mrcm.bean.CdkerResultData;
import com.hanbang.mrcm.utils.HttpInterfaces;
import com.hanbang.mrcm.utils.JsonHelper;
import com.hanbang.mrcm.utils.MyRequestCallBack;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class ResultActivity extends BaseActivity {

	private int activityId = -1;
	private int type = -1;
	private CdkerResultData cdkerResultData = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type = getIntent().getIntExtra("type", type);
		activityId = getIntent().getIntExtra("activity_id", activityId);
		setContentView(R.layout.cdker_result);
		initView();
		setOnClickListener();
		getCdker();

	}

	/*
	 * http://localhost:10080/tools/good_list.ashx?action=setexchangecode&mobile=
	 * 17051196428&password=123&activity_id=3&type=0 --直接领取
	 */
	private void getCdker() {
		String url = "";
		if (type == 2) {
			url = HttpInterfaces.getAnswerCode;
		} else if (type == 1 || type == 0) {
			url = HttpInterfaces.getCdker;
		}
		httpUtils.send(HttpMethod.GET, url + "&mobile=" + userData.getMobile()
				+ "&password=" + userData.getPassword() + "&activity_id="
				+ activityId + "&type=" + type, new MyRequestCallBack<String>(
				this, getString(R.string.loadding)) {
			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				super.onSuccess(responseInfo);
				try {
					/*
					 * {"status": 1, "msg": "领取成功！", "code": "8P0R2L2T",
					 * "activity_title": "dfsdfs", "expired_time":
					 * "2015/10/31 15:11:54"}
					 */
					JSONObject json = new JSONObject(responseInfo.result);
					int status = json.getInt("status");
					if (status == 1) {
						cdkerResultData = JsonHelper.parseObject(json,
								CdkerResultData.class);
						updataUI();
					} else if (status == -100) {
						passwordIsError(false);
					}

				} catch (JSONException e) {
					e.printStackTrace();
				}

			}

		});

	}

	private void updataUI() {
		if (cdkerResultData != null) {
			((TextView) findViewById(R.id.result)).setText(cdkerResultData
					.getMsg());
			((TextView) findViewById(R.id.cdker)).setText(cdkerResultData
					.getCode());
			((TextView) findViewById(R.id.title)).setText(cdkerResultData
					.getActivity_title());
			((TextView) findViewById(R.id.time)).setText(cdkerResultData
					.getExpired_time());
		}

	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {

	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			default:
				break;
			}
		}
	};

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setBackArrows(findViewById(R.id.top_bar_left_image));
		setTitle(findViewById(R.id.top_bar_title), "优惠券");

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
}
